﻿using System.Xml.Serialization;

namespace WebApplication1
{
    public class OrderService
    {
        public int count = 2;
        public List<Order> orders = new List<Order>
        {
            new Order(1,"David","WHU","134221",new OrderDetails(1,
                new List<Good>
                {
                    new Good(1,"A",1,5)
                }, 0, DateTime.Now)),
            new Order(2,"Mike","WHU","134222",new OrderDetails(2,
                new List<Good>
                {
                    new Good(2,"B",1,5)
                }, 0, DateTime.Now))
        };

        public OrderService()
        {

        }

        public OrderService(List<Order> orders)
        {
            this.orders = orders;
        }

        public Order IfExist(int id)
        {
            for(int i = 0; i < orders.Count; i++)
            {
                if(orders[i].Id == id)
                    return orders[i];
            }
            return null;
        }
        public bool IfExist(Order order)
        {
            for (int i = 0; i < orders.Count; i++)
            {
                if (orders[i].Equals(order))
                    return true;
            }
            return false;
        }

        public void Export(string filename)
        {
            XmlSerializer xmls = new XmlSerializer(typeof(List<Order>));
            using (FileStream fs = new FileStream(filename, FileMode.Create))
            {
                xmls.Serialize(fs, orders);
            }
        }

        public void Import(string filename)
        {
            XmlSerializer xmls = new XmlSerializer(typeof(List<Order>));
            using (FileStream fs = new FileStream(filename, FileMode.Open))
            {
                if (fs == null)
                {
                    Console.WriteLine("导入失败，文件为空");
                    return;
                }
                orders = xmls.Deserialize(fs) as List<Order>;
            }
        }

        public bool IfContain(Order order, string str)
        {
            if (order == null)
                return false;
            if (str == null)
                return true;
            if (order.Customer.Contains(str) || order.Address.Contains(str) || order.PhoneNumber.Contains(str))
                return true;
            if (order.Details.OrderDate.ToString().Contains(str))
                return true;
            for (int i = 0; i < order.Details.Goods.Count; i++)
                if (order.Details.Goods[i].Name.Contains(str))
                    return true;
            return false;

        }

        public List<Order> Query(string str)
        {
            var qon = orders.Where(i => IfContain(i, str)).OrderBy(i => i.Id);
            List<Order> ods = qon.ToList();
            return ods;
        }
        public List<Order> QuerySort(string str)
        {
            var qon = orders.Where(i => IfContain(i, str)).OrderByDescending(i => i.Details.Money());
            List<Order> ods = qon.ToList();
            return ods;
        }

        public void AddOrder(Order order)
        {
            if (order != null && !(IfExist(order)))
            {
                orders.Add(order);
                count++;
            }
        }



        public bool ChangeOrder(Order order, int id)
        {
            order.Id = id;
            for (int i = 0; i < orders.Count; i++)
            {
                if (orders[i].Id == id)
                {
                    orders[i] = order;
                    return true;
                }
            }
            return false;
        }
        public bool DeleteOrder(int id)
        {
            for (int i = 0; i < orders.Count; i++)
            {
                if (orders[i].Id == id)
                {
                    orders.Remove(orders[i]);
                    return true;
                }
            }
            return false;
        }

    }
}
